package com.newlink.building.activities

import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import androidx.core.content.FileProvider
import com.newlink.building.R
import java.io.File

/**
 * @Author: Jake
 * @Date: 2023/1/6
 * @Description:
 */
class Module_Phone_NotificationActivity : Module_Phone_GenericActivity() {

    companion object {
        var APP_PATH: String = "FVL_APP_PATH"
        var APP_NAME: String = "FVL_APP_NAME"
    }

    private var apkFile: File? = null
    private var appPath: String? = null
    private var appName: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        appPath = intent.getStringExtra(APP_PATH)
        appName = intent.getStringExtra(APP_NAME)

        installDownloadApk()
        finish()
    }

    private fun installDownloadApk() {
        apkFile = File(appPath, appName)
        apkFile?.let {
            if (!it.exists()) {
                return
            }
            val intent = Intent()
            intent.action = Intent.ACTION_VIEW

            val publicUri = FileProvider.getUriForFile(
                this,
                getString(R.string.file_provider),
                apkFile!!
            )

            intent.putExtra(MediaStore.EXTRA_OUTPUT, publicUri)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.setDataAndType(publicUri, "application/vnd.android.package-archive")

            startActivity(intent)
        }
    }
}
